Skip to content

Fix Avro required defaults#3254

Closed
koxudaxi wants to merge 1 commit into
mainfrom
fix-avro-required-defaults
Closed

Fix Avro required defaults#3254
koxudaxi wants to merge 1 commit into
mainfrom
fix-avro-required-defaults

Conversation

@koxudaxi

@koxudaxi koxudaxi commented Jun 4, 2026

Copy link
Copy Markdown
Owner

No description provided.

@coderabbitai

coderabbitai Bot commented Jun 4, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 6cd448b9-a7b7-4df5-9aa0-b90d68f9b59a

📥 Commits

Reviewing files that changed from the base of the PR and between 4131e7a and d74b53b.

📒 Files selected for processing (24)
  • src/datamodel_code_generator/__init__.py
  • src/datamodel_code_generator/parser/asyncapi.py
  • src/datamodel_code_generator/parser/avro.py
  • tests/data/expected/main/asyncapi/multi_format_schemas.py
  • tests/data/expected/main/avro/py310/constructs.py
  • tests/data/expected/main/avro/py310/official_long_list.py
  • tests/data/expected/main/avro/py310/official_schema_pass/record_fields_with_defaults.py
  • tests/data/expected/main/avro/py310/spec_matrix.py
  • tests/data/expected/main/avro/py311/constructs.py
  • tests/data/expected/main/avro/py311/official_long_list.py
  • tests/data/expected/main/avro/py311/official_schema_pass/record_fields_with_defaults.py
  • tests/data/expected/main/avro/py311/spec_matrix.py
  • tests/data/expected/main/avro/py312/constructs.py
  • tests/data/expected/main/avro/py312/official_long_list.py
  • tests/data/expected/main/avro/py312/official_schema_pass/record_fields_with_defaults.py
  • tests/data/expected/main/avro/py312/spec_matrix.py
  • tests/data/expected/main/avro/py313/constructs.py
  • tests/data/expected/main/avro/py313/official_long_list.py
  • tests/data/expected/main/avro/py313/official_schema_pass/record_fields_with_defaults.py
  • tests/data/expected/main/avro/py313/spec_matrix.py
  • tests/data/expected/main/avro/py314/constructs.py
  • tests/data/expected/main/avro/py314/official_long_list.py
  • tests/data/expected/main/avro/py314/official_schema_pass/record_fields_with_defaults.py
  • tests/data/expected/main/avro/py314/spec_matrix.py
💤 Files with no reviewable changes (1)
  • src/datamodel_code_generator/parser/avro.py

📝 Walkthrough

Walkthrough

This PR removes automatic propagation of Avro default values from schemas into generated Pydantic models. The change disables apply_default_values_for_required_fields in the Avro parser, eliminates AsyncAPI-specific handling for Avro defaults, and stops copying field-level defaults during Avro-to-JSON-Schema conversion. Test expectations across Python 3.10–3.14 are updated to reflect generated models without defaults.

Changes

Avro Default Values Removal

Layer / File(s) Summary
Core Avro default handling removal
src/datamodel_code_generator/__init__.py, src/datamodel_code_generator/parser/asyncapi.py, src/datamodel_code_generator/parser/avro.py
Avro parser configuration no longer forces apply_default_values_for_required_fields: True. AsyncAPI's apply_avro_default_values field and _avro_default_values context manager are removed. Avro-to-JSON-Schema conversion stops copying default values for record fields and enums.
Test expected outputs
tests/data/expected/main/asyncapi/multi_format_schemas.py, tests/data/expected/main/avro/py3{10,11,12,13,14}/*.py
Expected Pydantic model outputs across AsyncAPI and Avro fixtures are updated to remove default value assignments. Optional fields lose explicit = None defaults; fields with literal defaults (e.g., = 0, = True, = []) are declared without defaults, making them required at model construction.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • koxudaxi/datamodel-code-generator#3242: Previously added the apply_avro_default_values mechanism being removed here; directly coupled to the same AsyncAPI Avro default-handling code path.
  • koxudaxi/datamodel-code-generator#3054: Addressed required-field default rendering logic via use_default_with_required and __set_validate_default_on_fields, which interacts with the same default propagation behavior being removed.

Suggested labels

breaking-change, breaking-change-analyzed

Suggested reviewers

  • ilovelinux

Poem

🐰 A rabbit hops through schemas bright,
Defaults once set now fade from sight,
Avro fields stand bare and true,
No more magic—just what's due!
✨🥕

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 40.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: fixing how Avro required field defaults are handled in code generation.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix-avro-required-defaults

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions

github-actions Bot commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

📚 Docs Preview: https://pr-3254.datamodel-code-generator.pages.dev

@codspeed-hq

codspeed-hq Bot commented Jun 4, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 18.82%

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 11 improved benchmarks
⏩ 98 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
WallTime test_perf_all_options_enabled 6.4 s 5.2 s +22.17%
WallTime test_perf_deep_nested 5.9 s 4.9 s +21.49%
WallTime test_perf_kubernetes_style_pydantic_v2 2.9 s 2.4 s +20.37%
WallTime test_perf_aws_style_openapi_pydantic_v2 2.1 s 1.8 s +19.49%
WallTime test_perf_openapi_large 3.2 s 2.7 s +19.01%
WallTime test_perf_graphql_style_pydantic_v2 885.9 ms 746.3 ms +18.7%
WallTime test_perf_complex_refs 2.2 s 1.9 s +18.45%
WallTime test_perf_large_models_pydantic_v2 4 s 3.4 s +17.41%
WallTime test_perf_duplicate_names 1,115.7 ms 955.1 ms +16.81%
WallTime test_perf_multiple_files_input 3.8 s 3.3 s +16.81%
WallTime test_perf_stripe_style_pydantic_v2 2.3 s 1.9 s +16.45%

Tip

Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.


Comparing fix-avro-required-defaults (d74b53b) with main (4131e7a)

Open in CodSpeed

Footnotes

  1. 98 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@codecov

codecov Bot commented Jun 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (4131e7a) to head (d74b53b).

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #3254   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          112       112           
  Lines        25209     25194   -15     
  Branches      3185      3182    -3     
=========================================
- Hits         25209     25194   -15     
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@koxudaxi koxudaxi closed this Jun 4, 2026
@koxudaxi koxudaxi deleted the fix-avro-required-defaults branch June 21, 2026 14:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant